const { fetchEntries, fetchHtmls } = require('./util');
const { join, resolve } = require('path');
const CopyWebpackPlugin = require("copy-webpack-plugin");
const webpack = require('webpack');
const VueLoaderPlugin = require('vue-loader/lib/plugin');

const entries = fetchEntries(join(__dirname, '../src/entry'));
const staticRes = new CopyWebpackPlugin([{
	from: join(__dirname, '../static'),
	to: join(__dirname + '../dist')
}]);
module.exports = function () {
	return {
		mode: "development",
		devtool: 'cheap-module-eval-source-map',
		devServer: {
			clientLogLevel: 'info',
			disableHostCheck: true,
			// historyApiFallback:true,
			// historyApiFallback: {
			//      rewrites: [
			//        { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
			//      ],
			// },
			hot: false,
			contentBase: [join(__dirname, '../static'), join(__dirname, '../mock')],//false, // since we use CopyWebpackPlugin.
			compress: false,
			host: 'localhost',
			port: 8080,
			inline:true,
			open: false,
			overlay: { warnings: false, errors: true },
			publicPath: "/build",
			proxy: {
				// '/API':{
				// 	secure:false,
				// 	changeOrigin:true,
				// 	target:'http://localhost'
				// },
				// '/Project':{
				// 	secure:false,
				// 	changeOrigin:true,
				// 	target:'http://localhost'
				// }
			},//config.dev.proxyTable,
			quiet: true, // necessary for FriendlyErrorsPlugin
			watchOptions: {
				poll: false
			}
		},
		entry: entries,
		output: {
			publicPath: "../",
			path: join(__dirname, "../dist/build"),//打包后的文件存放的地方
			filename: "js/[name].[hash].js"//打包后输出文件的文件名
		},
		resolve: {
			modules:[join(__dirname,'../src/common/less'),join(__dirname,'../node_modules')],
			extensions: ['.js', '.vue', '.json', '.ts'],
			alias: {
				'vue$': 'vue/dist/vue.esm.js',
				'@': resolve('src'),
				"tsservice": resolve("src/service"),
				"tscomponents": resolve("src/components"),
				"tsmodules": resolve("src/modules"),
				"tscommon": resolve("src/common"),
				"tsdomain": resolve("src/store")
			}
		},
		externals: {
			vue: "Vue",
			vuex: "Vuex",
			"vue-router": "VueRouter",
			"axios": "axios",
			"element-ui": "ELEMENT",
			'vue-ydui': 'ydui'
		},
		module: {
			rules: [
				{
					test: /\.vue$/,
					loader: 'vue-loader',
					options: {
						loaders:
						{
							ts: ['babel-loader', 'ts-loader']
						},
						cssSourceMap: true,
						cacheBusting: true,
						transformToRequire: {
							video: ['src', 'poster'],
							source: 'src',
							img: 'src',
							image: 'xlink:href'
						}
					}
				},
				{
					test: /\.ts$/,
					use: [
						{
							loader: 'babel-loader'
						},
						{
							loader: 'ts-loader',
							options: {
								appendTsSuffixTo: [/\.vue$/]
							}
						}
					],
					exclude: /node_modules/
				},
				{
					test: /\.(less|css)/,
					use: [
						{
							loader: "style-loader"
						}, {
							loader: "css-loader",
							options: {
								modules: false
							}
						}, {
							loader: "postcss-loader"
						}, {
							loader: "less-loader"
						}
					]
				}, {
					test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
					loader: "url-loader?limit=8192&name=assets/[name].[hash].[ext]"
				}, {
					test: /\.(png|jpe?g)$/i,
					loaders: [
						{
							loader: 'url-loader',
							options: {
								name: 'img/[name].[hash].[ext]',
								limit: 8192
							}
						},
						{
							loader: 'image-webpack-loader',
							options: {
								progressive:true,
								optimizationLevel: 7,
								interlaced: false,
								pngquant:{quality: "65-90", speed: 4}
							}
						}						  
					]
				}
			]
		},
		plugins: [
			new webpack.HotModuleReplacementPlugin(),
			new VueLoaderPlugin(),
			staticRes,
			new webpack.BannerPlugin('zengxiao版权所有，翻版必究'),
			...fetchHtmls(entries, "development", '听写我帮忙之后台管理')
		]
	}
}